using System.Collections.Generic;
using System.IO;
using OracleInternal.Common;

namespace OracleInternal.Network
{
	internal class AuthenticationService : AnoService
	{
		internal const int NAU_OK = 64255;

		internal const int NAU_DONT_USE_AUTH = 64511;

		internal const int NAU_AUTH_NOT_REQUIRED = 64767;

		internal const int NAU_AUTH_REQUIRED = 65023;

		internal const int NAU_NO_DRIVERS_LINKED_IN = 65279;

		internal const int NAU_USE_IMPLICIT_AUTH = 63999;

		internal const int NAU_PROXY_NO_AUTH = 63743;

		internal const int NAU_AUTH_DISABLED = 63487;

		internal const int NAUCX_CLIENT_SERVER = 57569;

		internal static string[] AUTH_ORACLE_NAME;

		internal static string[] AUTH_ORACLE_NAME_nix;

		internal static string[] AUTH_ORACLE_NAME_win;

		internal static byte[] AUTH_ORACLE_ID;

		internal static byte[] AUTH_ORACLE_ID_nix;

		internal static byte[] AUTH_ORACLE_ID_win;

		internal bool m_authenticationActivated;

		internal int m_status;

		internal string m_authenticationService;

		static AuthenticationService()
		{
			AUTH_ORACLE_NAME_nix = new string[2]
			{
				"",
				"TCPS"
			};
			AUTH_ORACLE_NAME_win = new string[4]
			{
				"",
				"NTS",
				"KERBEROS5",
				"TCPS"
			};
			AUTH_ORACLE_ID_nix = new byte[2]
			{
				0,
				2
			};
			AUTH_ORACLE_ID_win = new byte[4]
			{
				0,
				1,
				1,
				2
			};
			AUTH_ORACLE_NAME = ((Path.DirectorySeparatorChar == '/') ? AUTH_ORACLE_NAME_nix : AUTH_ORACLE_NAME_win);
			AUTH_ORACLE_ID = ((Path.DirectorySeparatorChar == '/') ? AUTH_ORACLE_ID_nix : AUTH_ORACLE_ID_win);
		}

		internal AuthenticationService()
		{
		}

		internal override int Initialize(SessionContext sessCtx)
		{
			base.Initialize(sessCtx);
			m_service = 1;
			m_status = 64767;
			string[] userList = SqlNetOraConfig.MultipleValueSplit(sessCtx.m_conops.SNOConfig["sqlnet.authentication_services"]);
			userList = AnoService.ValidateUserChoiceDrivers(userList, AUTH_ORACLE_NAME, defaultIsAll: false);
			if (userList == null)
			{
				return 0;
			}
			m_userChoiceDriversId = new List<int>(userList.Length);
			for (int i = 0; i < userList.Length; i++)
			{
				m_userChoiceDriversId.Add(GetDriverID(AUTH_ORACLE_NAME, userList[i]));
			}
			return 1;
		}

		internal override int GetServiceDataLength()
		{
			int num = 20;
			if (m_userChoiceDriversId != null)
			{
				for (int i = 0; i < m_userChoiceDriversId.Count; i++)
				{
					num += 5;
					num += 4 + AUTH_ORACLE_NAME[m_userChoiceDriversId[i]].Length;
				}
			}
			return num;
		}

		internal override void SendServiceData()
		{
			int num = 3;
			if (m_userChoiceDriversId != null)
			{
				num += m_userChoiceDriversId.Count * 2;
			}
			SendHeader(num);
			m_anoComm.SendVersion();
			m_anoComm.SendUB2(57569);
			m_anoComm.SendStatus(m_status);
			if (m_userChoiceDriversId != null)
			{
				for (int i = 0; i < m_userChoiceDriversId.Count; i++)
				{
					m_anoComm.SendUB1(AUTH_ORACLE_ID[m_userChoiceDriversId[i]]);
					m_anoComm.SendString(AUTH_ORACLE_NAME[m_userChoiceDriversId[i]]);
				}
			}
		}

		internal override void ReceiveServiceData(int numSubPackets)
		{
			m_version = m_anoComm.ReceiveVersion();
			int num = m_anoComm.ReceiveStatus();
			if (num == 64255 && numSubPackets > 2)
			{
				m_anoComm.ReceiveUB1();
				string text = (m_authenticationService = m_anoComm.ReceiveString());
				if (numSubPackets > 4)
				{
					m_anoComm.ReceiveVersion();
					m_anoComm.ReceiveUB4();
					m_anoComm.ReceiveUB4();
				}
				m_authenticationActivated = true;
			}
			else
			{
				if (num != 64511)
				{
					throw new NetworkException(-6307);
				}
				m_authenticationActivated = false;
			}
		}

		internal override void ValidateResponse()
		{
			_ = m_authenticationActivated;
		}

		internal override void ActivateAlgorithm()
		{
		}
	}
}
